#include"public.h"

volatile   union ui_buff_ ui_buff[5];
s8 ui_id=6;
/*
const u8 nixienum[10]=
{
    0x3f,
    0x06,
    0x5b,
    0x4f,
    0x66,
    0x6d,
    0x7d,
    0x07,
    0x7f,
    0x6f
};
*/
volatile ui_ ui={0};

ui__ ui_ld={0};
volatile tim_ui_ tim_ui={0};
void new_nixie(void)
{
	ui_buff[0].a=ui.ts;
	if((ui.pw&2)==0)
	{
		ui_buff[0].b=0;
		ui_buff[2].a=0;
	}
	else
	{
		ui_buff[0].b=1;
		ui_buff[2].a=1;
	}
	if((ui.pw&1)==0)
	{
		ui_buff[0].c=0;
		ui_buff[2].b=0;
	}
	else
	{
		ui_buff[0].c=1;
		ui_buff[2].b=1;
	}
	if(ui.dian==4)
	{
		ui_buff[0].d=1;
		ui_buff[2].c=1;
	}
	else
	{
		ui_buff[0].d=0;
		ui_buff[2].c=0;
	}
	ui_buff[1].buf&=0xf8;
	ui_buff[3].buf&=0xfc;
	ui_buff[2].d=0;
	switch(ui.dian)
	{
	case 4:
	case 3:
		ui_buff[1].a=1;
		ui_buff[2].d=1;
	case 2:
		ui_buff[1].b=1;
		ui_buff[3].a=1;
	case 1:
		ui_buff[1].c=1;
		ui_buff[3].b=1;
		break;
	default:
		break;
	}
	ui_buff[1].d=ui.dian_s;
	ui_buff[3].buf&=0xf3;
	ui_buff[4].buf&=0xfc;
	
	switch(ui.you)
	{
	case 4:
		ui_buff[3].c=1;
	case 3:
		ui_buff[3].d=1;
	case 2:
		ui_buff[4].a=1;
	case 1:
		ui_buff[4].b=1;
		break;
	default:
		break;
	}
	ui_buff[4].c=ui.you_s;
	
	
}

void ldz(u8 a)
{
    ui_ld.w1    =a;
    ui_ld.w2 	=a;
    ui_ld.w3 	=a;
    ui_ld.w4 	=a;
}

//50ms 68
void ui_ca(task* task_)
{
    if(task_->sucCnt<=5)
    {
        switch(task_->sucCnt)
		{
		case 0:
			ui.you=0;
			ui.pw=0;
			ui.ts=0;
			ldz(ui_ldmax);
			ui.dian_s=1;
			ui.you_s=1;
			if(bat.dian>=1){ui.dian=1;}
			break;
		case 1:
			if(bat.dian>=2){ui.dian=2;}
			if(you>=1){ui.you=1;}
			break;
		case 2:
			if(bat.dian>=3){ui.dian=3;}
			if(you>=2){ui.you=2;}
			break;
		case 3:
			if(bat.dian>=4){ui.dian=4;}
			if(you>=3){ui.you=3;}
			break;
		case 4:
			if(you>=4){ui.you=4;}
			if(pw==1){ui.pw=1;}
			break;
		case 5:
			if(pw==0){ui.pw=2;}
			ui.ts=ts;
			break;
		}
    }
    else if(task_->sucCnt!=task_->maxSuc-1)
	{
		ui.dian=bat.dian;
		ui.you=you;
		ui.pw=2-pw;
    }
	else
	{
		ui.dian=0;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
	}
	new_nixie();
    if(task_->sucCnt==task_->maxSuc-1)
	{
        if(USB_DET&&(CHRG_FULL==0))
        {
            add_task_id(ui_chrg_id,ui_chrg_ms,ui_chrg_max,ui_id);
        }
    }
}
//50ms 12
void ui_ba(task* task_)
{
    if(task_->sucCnt<=5)
    {
		ui.dian=bat.dian;
		//ui.dian=4;
		ui.you=you;
		ui.dian_s=1;
		ui.you_s=1;
		ui.ts=ts;
		ui.pw=2-pw;
		ldz(ui_ldmax);
    }
    else
	{
		switch(task_->sucCnt)
		{
		case 6:
			ui.pw&=1;
			ui.ts=0;
			break;
		case 7:
			if(you>=4){ui.you=3;}
			ui.pw=0;
			break;
		case 8:
			if(bat.dian>=4){ui.dian=3;}
			if(you>=3){ui.you=2;}
			break;
		case 9:
			if(bat.dian>=3){ui.dian=2;}
			if(you>=2){ui.you=1;}
			break;
		case 10:
			if(bat.dian>=2){ui.dian=1;}
			ui.you=0;
			break;
		case 11:
			ui.dian=0;
			ui.dian_s=0;
			ui.you_s=0;
			break;
		}
    }
	new_nixie();
    if(task_->sucCnt==task_->maxSuc-1)
    {
        if(USB_DET&&(CHRG_FULL==0))
        {
            add_task_id(ui_chrg_id,ui_chrg_ms,ui_chrg_max,ui_id);
        }
    }
}


//500ms 0
void ui_chrg(task* task_)
{
    if(task_->sucCnt==0)
    {
        ui.dian=bat.dian;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=1;
		ui.you_s=0;
		ui.ts=ts;
		ui.pw=0;
		new_nixie();
        ldz(ui_ldmax);
    }
    task_->sucCnt=1;
	//ui.dian_s++;
	//ui.dian_s&=1;
	
    ui.dian++;
	if(ui.dian>4)
	{
		ui.dian=(bat.dian==4)?3:bat.dian;
	}
    new_nixie();
}

//250ms 7
void ui_chrgfree(task* task_)
{
    if(task_->sucCnt&1)
    {
        ui.dian=4;
		//ui.dian=4;
		ui.you=4;
		ui.dian_s=1;
		ui.you_s=1;
		ui.ts=1;
		ui.pw=3;
		ldz(ui_ldmax);
    }
    else
    {
        ui.dian=0;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
		ldz(0);
    }
    new_nixie();
}

//5000ms 3
void ui_chrgfull(task* task_)
{
    if(task_->sucCnt==0)
    {
        ui.dian=4;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=1;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
		ldz(ui_ldmax);
    }
    else if(task_->sucCnt==2)
    {
        ui.dian=0;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
		ldz(0);
    }
	new_nixie();
}

//50ms 0
void ui_smoing(task* task_)
{
    if(task_->sucCnt==0)
    {
        ui.dian=bat.dian;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
		ldz(0);
		new_nixie();
    }
	
    switch(ui.dian)
	{
	case 4:
		ui_ld.w1=ui_ld.w2;
	case 3:
		ui_ld.w2=ui_ld.w3;
	case 2:
		ui_ld.w3=ui_ld.w4;
	case 1:
		if(task_->sucCnt<=ui_ldmax)
		ui_ld.w4=ui_ldmax-task_->sucCnt+1;
		else
		ui_ld.w4=0;
		break;
	default:
		break;
	}
    task_->sucCnt++;
    if(task_->sucCnt>=ui_ldmax+ui_ld0max+1)
    {
        task_->sucCnt=1;
    }
}

//50ms 104or104or62or104
void ui_smoend_getup_dl_ts_off(task* task_)
{
    if(task_->sucCnt==0)
    {
        
            //ui.dian=bat.dian;
			//ui.dian=4;
			//ui.you=0;
			ui.dian_s=1;
			ui.you_s=1;
			ui.ts=ts;
			//ui.pw=0;
            ldz(ui_ldmax);
        
    }
	else if(task_->sucCnt==task_->maxSuc-1)
	{
		ui.dian=0;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
	}
    if((task_->sucCnt&1)==0)
    {
        ui.dian=bat.dian;
		ui.you=you;
		ui.pw=2-pw;
    }
    new_nixie();
    if(task_->sucCnt==task_->maxSuc-1)
    {
        if(USB_DET&&(CHRG_FULL==0))
        {
            add_task_id(ui_chrg_id,ui_chrg_ms,ui_chrg_max,ui_id);
        }
    }
}

//250ms 9 or 11 or 21
void ui_kl_timout_lowbat(task* task_)
{
    if(task_->sucCnt&1)
    {
        ui.dian=bat.dian;
		//ui.dian=4;
		ui.you=you;
		ui.dian_s=1;
		ui.you_s=1;
		ui.ts=ts;
		ui.pw=2-pw;
		ldz(ui_ldmax);
    }
    else
    {
        ui.dian=0;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
		ldz(0);
    }
    new_nixie();
}

//5000ms 2
void ui_ts_on(task* task_)
{
    if(task_->sucCnt==0)
    {
        ui.dian=0;
		//ui.dian=4;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=1;
		ui.pw=0;
		ldz(0);
    }
    else
	{
		ui.ts=0;
	}
    new_nixie();
    if(task_->sucCnt==task_->maxSuc-1)
    {
        if(USB_DET&&(CHRG_FULL==0))
        {
            add_task_id(ui_chrg_id,ui_chrg_ms,ui_chrg_max,ui_id);
        }
    }
}
/*
//50ms 43
void ui_ts_off(task* task_)
{
    if(task_->sucCnt<=9)
    {
        if(task_->sucCnt==0)
        {
            ui.w	    =1;
            ui.u	    =1;
            ui.q	    =10;
                
            ui.ou_d	    =0;
            ui.wa	    =1;
            ui.ts	    =0;
            ui.yd	    =yd;
            // ui.dian     =bat.dian;
            ui.cnt      =0;
            ldz(0);

            // ui.num1	    =10;
            // ui.num2	    =10;
        }
        ldz(task_->sucCnt+1);
    }
    else
    {
        if(task_->sucCnt==42)
        {
            ldz(0);
        }
    }
    if((task_->sucCnt&1)==0)
    {
        ui.num1	    =pw/10;
        ui.num2	    =pw-ui.num1*10;
        ui.dian	    =bat.dian;
    }
    new_nixie();
    if(task_->sucCnt==42)
    {
        if(USB_DET&&(CHRG_FULL==0))
        {
            add_task_id(ui_chrg_id,ui_chrg_ms,ui_chrg_max,ui_id);
        }
    }
}
*/

//300ms  3
void ui_begin(task* task_)
{
    if(task_->sucCnt==0)
    {
        ui.dian=4;
		//ui.dian=4;
		ui.you=4;
		ui.dian_s=1;
		ui.you_s=1;
		ui.ts=1;
		ui.pw=3;
		ldz(ui_ldmax);

    }
    else  if(task_->sucCnt==1)
    {
        ui.dian=0;
		ui.you=0;
		ui.dian_s=0;
		ui.you_s=0;
		ui.ts=0;
		ui.pw=0;
		ldz(0);
    }
    else
    {
        #if bug
        uart_txln("sleep");
        #endif
		sleep_mode();
        gpio_init();
			//__delay_ms(50);
			//TRISA0=0;
			//RA0=0;
			timer0_init();
			//timer1_init();
			cmp_init();
			//timer2_init();
			pwmpin_init();
            #if bug
            uart_init();
            #endif
            //uart_init();
            //__delay_ms(50);
	
		cb_init();
    }
    new_nixie();
}


